package nowcoder.course;

import common.TreeNode;
import common.Util;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;

/**
 * @author fance
 * @date 18-7-9 上午10:26
 */
public class Three {
    public boolean chkIdentical(TreeNode A, TreeNode B) {
        return preOrder(A).contains(preOrder(B));
    }
    private String preOrder(TreeNode root) {
        if (root == null) {
            return "#";
        }
        return root.val + " " + preOrder(root.left) + preOrder(root.right);
    }

    // 1.sort 2.table
    public boolean chkTransform(String A, int lena, String B, int lenb) {
        if (A == null || B == null || lena != lenb) {
            return false;
        }
        int[] map = new int[256];
        for (int i = 0; i < lena; i++) {
            map[A.charAt(i)]++;
        }
        for (int i = 0; i < lenb; i++) {
            if (map[B.charAt(i)]-- == 0) {
                return false;
            }
        }
        return true;
    }


    public String reverseSentence(String A, int n) {
        if (A == null || n == 0) {
            return null;
        }
        char[] a = A.toCharArray();
        int l = 0;
        int r = 0;
        while (l < n && a[l] == ' ') {
            l++;
        }
        r = l;
        while (r < n && a[r] != ' ') {
            r ++;
        }
        if (l != r) {
            Util.reverse(a,l,r);
            l = r;
        }
        Util.reverse(a,0,n - 1);
        return  String.valueOf(a);
    }

    public String stringTranslation(String A, int n, int len) {
        int step = len % n;
        char[] a = A.toCharArray();
        Util.reverse(a,0,step - 1);
        Util.reverse(a,step,n - 1);
        Util.reverse(a,0,n - 1);
        return String.valueOf(a);
    }

    public String findSmallest(String[] strs, int n) {
        if (strs == null || n < 1) {
            return "";
        }
        Arrays.sort(strs, (String o1, String o2) -> {
            return (o1 + o2).compareTo(o2 + o1);
        });
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < n; i++) {
            res.append(strs[i]);
        }
        return res.toString();
    }

    public String replaceSpace(String iniString, int length) {
        return iniString.replaceAll(" ", "%20");
    }
    public String replaceSpaceII(String iniString, int length) {
        int spaceCnt = 0;
        for (int i = 0; i < length; i++) {
            if (iniString.charAt(i) == ' ') {
                spaceCnt++;
            }
        }
        if (spaceCnt == 0) {
            return iniString;
        }

        char[] temp = new char[length + 2 * spaceCnt];
        int j = temp.length - 1;
        int i = length - 1;
        while (i >= 0) {
            if (iniString.charAt(i) == ' ') {
                temp[j--] = '0';
                temp[j--] = '2';
                temp[j--] = '%';
                i--;
            } else {
                temp[j--] = iniString.charAt(i--);
            }
        }
        return new String(temp);
    }

    public boolean chkParenthesis(String A, int n) {
        if (A == null || n < 2) {
            return false;
        }
        LinkedList<Character> stack = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            char c = A.charAt(i);
            if (c == '(') {
                stack.push(c);
            } else if (c == ')') {
                if (stack.isEmpty()) {
                    return false;
                }
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
    public int longestSubstring(String s, int n) {
        if (s == null || n < 1) {
            return 0;
        }
        Set<Character> set = new HashSet<>();
        int ans = 0,i = 0, j = 0;
        while (i < n && j < n) {
            if (!set.contains(s.charAt(j))) {
                set.add(s.charAt(j++));
                ans = Math.max(ans,j - i);
            } else {
                set.remove(s.charAt(i++));
            }
        }
        return ans;
    }
    @Test
    public void test() {
        System.out.println(replaceSpaceII("Mr John Smith",13));
        System.out.println(replaceSpace("Mr John Smith",13));
//        System.out.println(stringTranslation("ABCDE",5,3));
    }
}
